//
//  BooksFileUtil.m
//  handbooklite
//
//  Created by bao_wsfk on 12-8-31.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "BooksFileUtil.h"
#import "Config.h"
#import "JSONKit.h"

@implementation BooksFileUtil

@synthesize fm =_fm;
@synthesize booksFilePath =_booksFilePath;

static BooksFileUtil *_instance;

+ (id)newInstance{
    
    @synchronized(self){
        if (!_instance) {
            _instance =[[BooksFileUtil alloc] init];
            [_instance setBooksFilePath:[DOCUMENT_PATH stringByAppendingPathComponent:@"books/books.json"]];
        }
        return _instance;
    }
}

- (id)init{
    
    if (self =[super init]) {
        _fm =[[NSFileManager alloc] init];
    }
    return self;
}

- (void)dealloc{
    [super dealloc];
    _fm =nil;
    _booksFilePath =nil;
}

- (void)setBooksFilePath:(NSString *)booksFilePath{
    _booksFilePath =[booksFilePath copy];
}

- (BOOL)isHasFile{
    return [_fm fileExistsAtPath:_booksFilePath];
}

- (BOOL)isHasDownNum:(NSString *)downnum{
    if (![self isHasFile]) {
        return NO;
    }else {
        /**     {"books":[
                            {"123":{"icon":"icons/123.png","name":"时尚家居","dir":"123"}},
                            {"234":{"icon":"icons/234.png","name":"时尚家居2","dir":"234"}}
                         ]}
         
         */
        NSDictionary *booksDic =[[NSData dataWithContentsOfFile:_booksFilePath] objectFromJSONData];
        NSArray *books =[booksDic objectForKey:@"books"];
        for (NSDictionary *bookDic in books) {
            if ([booksDic objectForKey:downnum] !=nil) {
                return YES;
            }
        }
    }
    return NO;
}

- (BOOL)addBooksName:(NSString *)name icon:(NSString *)icon downnum:(NSString *)downnum{
    //判断是否存在books.json
    icon =[NSString stringWithFormat:@"icons/%@",icon];
    if (![self isHasFile]) {
        //不存在创建
        NSDictionary *book =[NSDictionary dictionaryWithObjectsAndKeys:
                             icon,@"icon",name,@"name",downnum,@"dir",nil];
        NSDictionary *bookDic =[NSDictionary dictionaryWithObject:book forKey:downnum];
        NSArray *books =[NSArray arrayWithObject:bookDic];
        NSDictionary *booksDic =[NSDictionary dictionaryWithObject:books forKey:@"books"];
        
        return [[booksDic JSONData] writeToFile:_booksFilePath atomically:YES];
    }else {
        //判断是否存在downnum
        if (![self isHasDownNum:downnum]) {
            //不存在添加
            NSMutableDictionary *booksDic =[[[NSData dataWithContentsOfFile:_booksFilePath]
                                             objectFromJSONData] mutableCopy];
            NSMutableArray *books =[[booksDic objectForKey:@"books"] mutableCopy];
            
            NSDictionary *book =[NSDictionary dictionaryWithObjectsAndKeys:
                                 icon,@"icon",name,@"name",downnum,@"dir",nil];
            NSDictionary *bookDic =[NSDictionary dictionaryWithObject:book forKey:downnum];
            
            [books addObject:bookDic];
            [booksDic setObject:books forKey:@"books"];
            return [[booksDic JSONData]writeToFile:_booksFilePath atomically:YES];
        }
    }
    return NO;
}

- (NSArray *)books{
    return [[[NSData dataWithContentsOfFile:_booksFilePath] objectFromJSONData] objectForKey:@"books"];
}

- (NSDictionary *)bookByDownNum:(NSString *)downnum{
    
    for (NSDictionary *book in [self books]) {
        /**
         ["123":{"icon":"icon.jpg","name","时尚家居","dir":"123"}]
         */
        if ([book objectForKey:downnum] !=nil) {
            return [book objectForKey:downnum];
        }
    }
    return nil;
}

@end
